package uz.edo.app.admin.rolePages;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListMultipleChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import uz.edo.app.admin.AdminPage;
import uz.edo.db.bean.Privilege;
import uz.edo.db.bean.Role;
import uz.edo.db.manager.PrivilegeManager;
import uz.edo.db.manager.RoleManager;
import uz.edo.utils.Constants;

/**
 * Created by IntelliJ IDEA.
 * User: said
 * Date: 1/23/12
 * Time: 5:15 AM
 * To change this template use File | Settings | File Templates.
 */
public class RoleAdd extends AdminPage{

    private RoleManager manager = RoleManager.getInstance();
    private PrivilegeManager privilegeManager = PrivilegeManager.getInstance();

    public RoleAdd() {
        final Role role = new Role();
        CompoundPropertyModel model = new CompoundPropertyModel(role);
        Form form = new Form("form",model){
            @Override
            protected void onSubmit() {
                try {
                    manager.saveOrUpdate(role);
                    setResponsePage(RoleListView.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        add(form);

        form.add(new TextField("name"));
        form.add(new TextField("description"));
        try {
            form.add(new ListMultipleChoice<Privilege>("privilegeList", isPrivileged("su") ? privilegeManager.getAll() : privilegeManager.getPrivileges(), Constants.RENDER));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
